package cn.tedu.lucky.draw.server.job.config;

import org.springframework.batch.core.*;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HelloJobConfig {

    /**
     * job 构建工厂
     * 用于去构建Job对象
     */

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    /**
     * Step 构建工厂
     * 用于去构建Step对象
     */
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    //作业启动器
    @Autowired
    private JobLauncher jobLauncher;

    //构建一个Job对象
    @Bean
    public Job job(){
        return jobBuilderFactory
                .get("hello-job2")
                .start(step())
                .build();
    }


    //构建一个step对象
    @Bean
    public Step step(){


        /**
         * tasklet 指定step执行逻辑，类似于Thread
         */
        return stepBuilderFactory.get("step")
                .tasklet((stepContribution, chunkContext) -> {
                    System.out.println("Hello,Spring Batch!");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }
}
